// a library to wrap and simplify api calls
import apisauce from 'apisauce'

// our "constructor"
const create = (baseURL = 'http://www.jiankangdaowei.com/') => {
  // ------
  // STEP 1
  // ------
  //
  // Create and configure an apisauce-based api object.
  //
  const api = apisauce.create({
    // base URL is read from the "constructor"
    baseURL,
    // here are some default headers
    headers: {
      'Cache-Control': 'no-cache'
    },
    // 10 second timeout...
    timeout: 10000
  })

  // location service
  const amapKey = '7394b8df90d413c5c31541eaf108922b'
  const amapApi = apisauce.create({
    baseURL: 'http://restapi.amap.com/v3/',
    headers: {
      'Cache-Control': 'no-cache'
    },
    timeout: 10000
  })
  const getcity = (longitude, latitude) => amapApi.get('geocode/regeo',{
    key: amapKey,
    location: `${longitude},${latitude}`,
    poitype: '商务住宅|住宿服务',
    radius: 1000,
    extensions: 'base',
    batch: false,
    roadlevel: 0
  })

  // ------
  // STEP 2
  // ------
  //
  // Define some functions that call the api.  The goal is to provide
  // a thin wrapper of the api layer providing nicer feeling functions
  // rather than "get", "post" and friends.
  //
  // I generally don't like wrapping the output at this level because
  // sometimes specific actions need to be take on `403` or `401`, etc.
  //
  // Since we can't hide from that, we embrace it by getting out of the
  // way at this level.
  //
  //const getRoot = () => api.get('')
  const getcomment = (id) => api.get('api/master/comment',{id:id})
  const getcoupon = (token) => api.get('api/order/coupon', {}, {headers:{'token':token}})
  const getuser = (token, deviceId) => api.get('api/user/index',{deviceId:deviceId},{headers:{'token':token}})
  const updateuser = (token,field,value) => api.post('api/user/update_user',{field:field,val:value},{headers:{'token':token}})
  const login = (mobile,password,deviceId) => api.post('api/login/login', {mobile, password, deviceId})
  const getvalidcode = (mobile) => api.get('api/login/send_verify', {mobile:mobile})
  const getcheckMobile = (mobile) => api.post('api/login/mobile_verify',{mobile:mobile})
  const getregister = (mobile,password,deviceId,longitude,latitude) => api.post('api/login/register', {mobile,password,deviceId,longitude,latitude})
  const changePassword = (password) => api.post('api/user/password', {password:password})

  const getAd = (code, adcode) => api.get('api/index/ad', {code:code,adcode:adcode})
  const getcommodityCategory = () => api.get('api/index/category')
  const getcommodity = (categoryId) => api.get('api/index/project', {category_id:categoryId})

  // wait remove
  const getCommodityCategoryList = () => api.get('api/index/category')
  const getCommodityList = (category) => api.get('api/index/project', {
    category_id:category})
  const getcommodityDetail = (id,adcode) => api.get('api/index/getOneProject', {id:id,adcode:adcode})
  const getserver = (token,city,longitude,latitude,filterType,filterSort) => api.get('api/master/lists',{city,longitude,latitude,filterType,filterSort},{headers:{'token':token}})
  const getserverDetail = (id) => api.get('api/master/artificer', {id:id})
  const getserverCommodity = (id,adcode) => api.get('api/master/artificer_project',{id:id,adcode:adcode})

  const getdefaultAddress = (token) => api.get('api/user/get_address',{},{headers:{'token':token}})


  // address
  const getAllAddress = (token) => api.get('api/user/address_list', {},{headers:{'token':token}})
  const createAddress = (token,data) => api.post('api/user/add_address',data,{headers:{'token':token}})
  const updateAddress = (token,data)=>api.post('api/user/edit_address',data,{headers:{'token':token}})
  const deleteAddress = (token,id)=>api.get('api/user/del_address',{id:id},{headers:{'token':token}})


  const getcall = ({commodityId,address,longitude,latitude,couponId,token}) => api.post('api/order/call', {project_id:commodityId,address:address,longitude:longitude,latitude:latitude,coupon_id:couponId},{headers:{'token':token}})
  const handleOrder = (id, status, token) => api.post('api/user/order_operate', {id:id,status:status}, {headers:{'token':token}})
  const getorder= (token, code) => api.get('api/user/order_list',{status:code},{headers:{'token':token}})
  const getorderDetail = (id,token) => api.get('api/user/order_detail',{id:id},{headers:{'token':token}})
  const cancelOrder = (id,token) => api.post('api/user/order_cancel',{id:id},{headers:{'token':token}})
  const deleteOrder = (id,token) => api.post('api/user/order_del',{id:id},{headers:{'token':token}})

  const getpromotion = (token) => api.get('api/user/share2', {}, {headers:{'token':token}})
  const getpage = (adcode) => api.get('api/index/articles/',{adcode:adcode})
  const gettodayRecommend = (adcode) => api.get('api/index/recommend',{adcode:adcode})
  const getpay = (token,id,status) => api.post('api/user/order_operate',{id:id,status:status},{headers:{'token':token}})
  const getalipay = (token,id,payType) => api.post('api/payment/get_pay',{order_id:id,pay_radio:payType},{headers:{'token':token}})

  const getvip = () => api.get('api/index/enjoy')
  const getwallet = (token) => api.get('api/user/account',{},{headers:{'token':token}})


  // ------
  // STEP 3
  // ------
  //
  // Return back a collection of functions that we would consider our
  // interface.  Most of the time it'll be just the list of all the
  // methods in step 2.
  //
  // Notice we're not returning back the `api` created in step 1?  That's
  // because it is scoped privately.  This is one way to create truly
  // private scoped goodies in JavaScript.
  //
  return {
    // a list of the API functions from step 2
    getcomment,
    getcoupon,
    getuser,
    getvalidcode,
    getcheckMobile,
    login,
    getregister,
    changePassword,
    updateuser,

    getAd,
    getcommodity,
    getcommodityDetail,
    getcommodityCategory,
    getCommodityCategoryList,
    getCommodityList,
    getserver,
    getserverDetail,
    getserverCommodity,
    getdefaultAddress,

    // address
    getAllAddress,
    createAddress,
    updateAddress,
    deleteAddress,

    getcall,
    getorder,
    getorderDetail,
    cancelOrder,
    deleteOrder,
    getpromotion,
    getpage,
    gettodayRecommend,
    getpay,
    getalipay,
    getvip,
    getwallet,

    // amap service
    getcity,
  }
}

// let's return back our create method as the default.
export default {
  create
}
